
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 11/29/2013 07:47:05
-- Generated from EDMX file: D:\Pntech\SourceCode\PNProject\PNProject\EF\PNModel.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [PNDatabase];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_ImagesItem_Item]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ImagesItem] DROP CONSTRAINT [FK_ImagesItem_Item];
GO
IF OBJECT_ID(N'[dbo].[FK_Item_Category]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Item] DROP CONSTRAINT [FK_Item_Category];
GO
IF OBJECT_ID(N'[dbo].[FK_ItemDetails_Item]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ItemDetails] DROP CONSTRAINT [FK_ItemDetails_Item];
GO
IF OBJECT_ID(N'[dbo].[FK_ItemProperty_Item]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ItemProperty] DROP CONSTRAINT [FK_ItemProperty_Item];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[Banner]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Banner];
GO
IF OBJECT_ID(N'[dbo].[Category]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Category];
GO
IF OBJECT_ID(N'[dbo].[Comment]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Comment];
GO
IF OBJECT_ID(N'[dbo].[Feedback]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Feedback];
GO
IF OBJECT_ID(N'[dbo].[File]', 'U') IS NOT NULL
    DROP TABLE [dbo].[File];
GO
IF OBJECT_ID(N'[dbo].[Image]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Image];
GO
IF OBJECT_ID(N'[dbo].[ImagesItem]', 'U') IS NOT NULL
    DROP TABLE [dbo].[ImagesItem];
GO
IF OBJECT_ID(N'[dbo].[Item]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Item];
GO
IF OBJECT_ID(N'[dbo].[ItemDetails]', 'U') IS NOT NULL
    DROP TABLE [dbo].[ItemDetails];
GO
IF OBJECT_ID(N'[dbo].[ItemProperty]', 'U') IS NOT NULL
    DROP TABLE [dbo].[ItemProperty];
GO
IF OBJECT_ID(N'[dbo].[Libraries]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Libraries];
GO
IF OBJECT_ID(N'[dbo].[Links]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Links];
GO
IF OBJECT_ID(N'[dbo].[News]', 'U') IS NOT NULL
    DROP TABLE [dbo].[News];
GO
IF OBJECT_ID(N'[dbo].[ObjectImage]', 'U') IS NOT NULL
    DROP TABLE [dbo].[ObjectImage];
GO
IF OBJECT_ID(N'[dbo].[OneContent]', 'U') IS NOT NULL
    DROP TABLE [dbo].[OneContent];
GO
IF OBJECT_ID(N'[dbo].[Setting]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Setting];
GO
IF OBJECT_ID(N'[dbo].[Support]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Support];
GO
IF OBJECT_ID(N'[dbo].[sysdiagrams]', 'U') IS NOT NULL
    DROP TABLE [dbo].[sysdiagrams];
GO
IF OBJECT_ID(N'[dbo].[TBEmail]', 'U') IS NOT NULL
    DROP TABLE [dbo].[TBEmail];
GO
IF OBJECT_ID(N'[dbo].[UserTracking]', 'U') IS NOT NULL
    DROP TABLE [dbo].[UserTracking];
GO
IF OBJECT_ID(N'[dbo].[XUser]', 'U') IS NOT NULL
    DROP TABLE [dbo].[XUser];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'Categories'
CREATE TABLE [dbo].[Categories] (
    [CategoryID] int IDENTITY(1,1) NOT NULL,
    [CategoryName] nvarchar(255)  NULL,
    [CategoryNameEN] nvarchar(255)  NULL,
    [ParentID] int  NULL,
    [Description] nvarchar(max)  NULL,
    [DescriptionEN] nvarchar(max)  NULL,
    [TLink] varchar(255)  NULL,
    [TLinkEN] varchar(255)  NULL,
    [Lang] bit  NULL
);
GO

-- Creating table 'Comments'
CREATE TABLE [dbo].[Comments] (
    [CommentID] uniqueidentifier  NOT NULL,
    [Type] int  NULL,
    [TargetID] varchar(50)  NULL,
    [Phone] varchar(50)  NULL,
    [Email] varchar(255)  NULL,
    [Address] nvarchar(255)  NULL,
    [Message] nvarchar(max)  NULL,
    [SentDate] datetime  NULL,
    [IsChecked] bit  NULL,
    [Username] nvarchar(256)  NULL
);
GO

-- Creating table 'Files'
CREATE TABLE [dbo].[Files] (
    [FileID] uniqueidentifier  NOT NULL,
    [FileName] varchar(max)  NULL,
    [Description] nvarchar(max)  NULL,
    [Timestamp] bigint  NULL
);
GO

-- Creating table 'Images'
CREATE TABLE [dbo].[Images] (
    [ImageID] uniqueidentifier  NOT NULL,
    [FileName] varchar(max)  NULL,
    [Description] nvarchar(max)  NULL,
    [Timestamp] bigint  NULL
);
GO

-- Creating table 'ItemProperties'
CREATE TABLE [dbo].[ItemProperties] (
    [PropertyID] uniqueidentifier  NOT NULL,
    [ItemID] uniqueidentifier  NULL,
    [PKey] varchar(255)  NOT NULL,
    [PValue] nvarchar(max)  NULL
);
GO

-- Creating table 'ObjectImages'
CREATE TABLE [dbo].[ObjectImages] (
    [ObjectImageID] uniqueidentifier  NOT NULL,
    [URL] nvarchar(max)  NULL,
    [ObjectID] nvarchar(50)  NULL,
    [Sort] int  NULL
);
GO

-- Creating table 'Settings'
CREATE TABLE [dbo].[Settings] (
    [SettingID] int IDENTITY(1,1) NOT NULL,
    [SKey] nvarchar(255)  NULL,
    [SValue] nvarchar(max)  NULL
);
GO

-- Creating table 'sysdiagrams'
CREATE TABLE [dbo].[sysdiagrams] (
    [name] nvarchar(128)  NOT NULL,
    [principal_id] int  NOT NULL,
    [diagram_id] int IDENTITY(1,1) NOT NULL,
    [version] int  NULL,
    [definition] varbinary(max)  NULL
);
GO

-- Creating table 'Supports'
CREATE TABLE [dbo].[Supports] (
    [SupportID] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(50)  NULL,
    [Description] nvarchar(255)  NULL,
    [Nick] nvarchar(50)  NULL,
    [Type] bit  NULL
);
GO

-- Creating table 'Banners'
CREATE TABLE [dbo].[Banners] (
    [BannerID] int IDENTITY(1,1) NOT NULL,
    [URL] varchar(max)  NULL,
    [URLEN] varchar(max)  NULL,
    [Description] nvarchar(max)  NULL,
    [DescriptionEN] nvarchar(max)  NULL,
    [Lang] bit  NULL,
    [Arrang] int  NULL,
    [Type] int  NULL
);
GO

-- Creating table 'UserTrackings'
CREATE TABLE [dbo].[UserTrackings] (
    [UserTrackID] uniqueidentifier  NOT NULL,
    [Username] nvarchar(256)  NULL,
    [CreateDate] datetime  NULL,
    [Description] nvarchar(max)  NULL,
    [Type] int  NULL,
    [TargetID] varchar(255)  NULL
);
GO

-- Creating table 'XUsers'
CREATE TABLE [dbo].[XUsers] (
    [UserID] uniqueidentifier  NOT NULL,
    [Username] nvarchar(256)  NULL,
    [Name] nvarchar(255)  NULL,
    [Email] varchar(255)  NULL,
    [Address] nvarchar(max)  NULL,
    [Info] nvarchar(max)  NULL,
    [Phone] varchar(50)  NULL,
    [Birthday] nvarchar(50)  NULL,
    [Avatar] nvarchar(255)  NULL,
    [IsBlocked] bit  NULL,
    [Role] nvarchar(50)  NULL
);
GO

-- Creating table 'Feedbacks'
CREATE TABLE [dbo].[Feedbacks] (
    [FeedbackID] uniqueidentifier  NOT NULL,
    [Email] varchar(255)  NULL,
    [Name] nvarchar(255)  NULL,
    [Phone] varchar(50)  NULL,
    [Title] nvarchar(255)  NULL,
    [Message] nvarchar(max)  NULL,
    [IsRead] bit  NULL,
    [SentDate] datetime  NULL,
    [ReplyBy] varchar(255)  NULL,
    [ReplyDate] datetime  NULL,
    [ReplyMessage] nvarchar(max)  NULL,
    [Type] int  NULL,
    [Address] nvarchar(255)  NULL,
    [Lang] bit  NULL,
    [Province] nvarchar(255)  NULL,
    [Country] nvarchar(255)  NULL
);
GO

-- Creating table 'OneContents'
CREATE TABLE [dbo].[OneContents] (
    [OneContentID] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(255)  NULL,
    [NameEN] nvarchar(255)  NULL,
    [TLink] nvarchar(255)  NULL,
    [TLinkEN] nvarchar(255)  NULL,
    [XContent] nvarchar(max)  NULL,
    [XContentEN] nvarchar(max)  NULL,
    [Lang] bit  NULL,
    [Extra_string] nvarchar(max)  NULL,
    [Extra_int] int  NULL
);
GO

-- Creating table 'Links'
CREATE TABLE [dbo].[Links] (
    [LinkID] int IDENTITY(1,1) NOT NULL,
    [TextLink] nvarchar(max)  NULL,
    [URL] varchar(max)  NULL,
    [Image] varchar(max)  NULL,
    [Title] nvarchar(max)  NULL,
    [Lang] bit  NULL,
    [Type] bit  NULL
);
GO

-- Creating table 'News'
CREATE TABLE [dbo].[News] (
    [NewsID] uniqueidentifier  NOT NULL,
    [Title] nvarchar(max)  NULL,
    [TLink] nvarchar(max)  NULL,
    [TitleEN] nvarchar(max)  NULL,
    [TLinkEN] nvarchar(max)  NULL,
    [Short] nvarchar(max)  NULL,
    [ShortEN] nvarchar(max)  NULL,
    [ContentVN] nvarchar(max)  NULL,
    [ContentEN] nvarchar(max)  NULL,
    [CType] int  NULL,
    [Lang] bit  NULL,
    [CreateDate] datetime  NULL,
    [CreateBy] nvarchar(256)  NULL,
    [LastUpdate] datetime  NULL,
    [UpdateBy] nvarchar(256)  NULL,
    [Avatar] varchar(max)  NULL,
    [ViewCount] int  NULL,
    [AttachFiles] nvarchar(max)  NULL,
    [CommentAllow] bit  NULL,
    [IsChecked] bit  NULL
);
GO

-- Creating table 'Libraries'
CREATE TABLE [dbo].[Libraries] (
    [ElementID] uniqueidentifier  NOT NULL,
    [Title] nvarchar(1000)  NULL,
    [URL] nvarchar(4000)  NULL,
    [Type] int  NOT NULL,
    [Lang] bit  NOT NULL,
    [IsPublic] bit  NOT NULL,
    [ViewCount] int  NOT NULL,
    [CreateDate] datetime  NOT NULL
);
GO

-- Creating table 'TBEmails'
CREATE TABLE [dbo].[TBEmails] (
    [EmailID] int IDENTITY(1,1) NOT NULL,
    [Email] varchar(255)  NULL,
    [Enable] bit  NULL,
    [Type] bit  NULL
);
GO

-- Creating table 'ImagesItems'
CREATE TABLE [dbo].[ImagesItems] (
    [Id] uniqueidentifier  NOT NULL,
    [ItemId] uniqueidentifier  NULL,
    [CreatedDate] datetime  NULL,
    [ModifiedDate] datetime  NULL,
    [Url] nvarchar(3000)  NULL,
    [Language] int  NULL,
    [OrderNumber] int IDENTITY(1,1) NOT NULL
);
GO

-- Creating table 'Items'
CREATE TABLE [dbo].[Items] (
    [ItemID] uniqueidentifier  NOT NULL,
    [CategoryID] int  NULL,
    [ItemName] nvarchar(max)  NULL,
    [ItemNameEN] nvarchar(max)  NULL,
    [CreateBy] varchar(255)  NULL,
    [LastUpdate] datetime  NULL,
    [UpdateBy] varchar(255)  NULL,
    [TLink] varchar(max)  NULL,
    [TLinkEN] varchar(max)  NULL,
    [Lang] bit  NULL,
    [ItemType] int  NULL,
    [IsDelete] bit  NULL
);
GO

-- Creating table 'ItemDetails'
CREATE TABLE [dbo].[ItemDetails] (
    [Id] uniqueidentifier  NOT NULL,
    [ItemId] uniqueidentifier  NULL,
    [Description] nvarchar(3000)  NULL,
    [Type] int  NULL,
    [CreatedDate] datetime  NULL,
    [ModifiedDate] datetime  NULL,
    [Details] nvarchar(max)  NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [CategoryID] in table 'Categories'
ALTER TABLE [dbo].[Categories]
ADD CONSTRAINT [PK_Categories]
    PRIMARY KEY CLUSTERED ([CategoryID] ASC);
GO

-- Creating primary key on [CommentID] in table 'Comments'
ALTER TABLE [dbo].[Comments]
ADD CONSTRAINT [PK_Comments]
    PRIMARY KEY CLUSTERED ([CommentID] ASC);
GO

-- Creating primary key on [FileID] in table 'Files'
ALTER TABLE [dbo].[Files]
ADD CONSTRAINT [PK_Files]
    PRIMARY KEY CLUSTERED ([FileID] ASC);
GO

-- Creating primary key on [ImageID] in table 'Images'
ALTER TABLE [dbo].[Images]
ADD CONSTRAINT [PK_Images]
    PRIMARY KEY CLUSTERED ([ImageID] ASC);
GO

-- Creating primary key on [PropertyID] in table 'ItemProperties'
ALTER TABLE [dbo].[ItemProperties]
ADD CONSTRAINT [PK_ItemProperties]
    PRIMARY KEY CLUSTERED ([PropertyID] ASC);
GO

-- Creating primary key on [ObjectImageID] in table 'ObjectImages'
ALTER TABLE [dbo].[ObjectImages]
ADD CONSTRAINT [PK_ObjectImages]
    PRIMARY KEY CLUSTERED ([ObjectImageID] ASC);
GO

-- Creating primary key on [SettingID] in table 'Settings'
ALTER TABLE [dbo].[Settings]
ADD CONSTRAINT [PK_Settings]
    PRIMARY KEY CLUSTERED ([SettingID] ASC);
GO

-- Creating primary key on [diagram_id] in table 'sysdiagrams'
ALTER TABLE [dbo].[sysdiagrams]
ADD CONSTRAINT [PK_sysdiagrams]
    PRIMARY KEY CLUSTERED ([diagram_id] ASC);
GO

-- Creating primary key on [SupportID] in table 'Supports'
ALTER TABLE [dbo].[Supports]
ADD CONSTRAINT [PK_Supports]
    PRIMARY KEY CLUSTERED ([SupportID] ASC);
GO

-- Creating primary key on [BannerID] in table 'Banners'
ALTER TABLE [dbo].[Banners]
ADD CONSTRAINT [PK_Banners]
    PRIMARY KEY CLUSTERED ([BannerID] ASC);
GO

-- Creating primary key on [UserTrackID] in table 'UserTrackings'
ALTER TABLE [dbo].[UserTrackings]
ADD CONSTRAINT [PK_UserTrackings]
    PRIMARY KEY CLUSTERED ([UserTrackID] ASC);
GO

-- Creating primary key on [UserID] in table 'XUsers'
ALTER TABLE [dbo].[XUsers]
ADD CONSTRAINT [PK_XUsers]
    PRIMARY KEY CLUSTERED ([UserID] ASC);
GO

-- Creating primary key on [FeedbackID] in table 'Feedbacks'
ALTER TABLE [dbo].[Feedbacks]
ADD CONSTRAINT [PK_Feedbacks]
    PRIMARY KEY CLUSTERED ([FeedbackID] ASC);
GO

-- Creating primary key on [OneContentID] in table 'OneContents'
ALTER TABLE [dbo].[OneContents]
ADD CONSTRAINT [PK_OneContents]
    PRIMARY KEY CLUSTERED ([OneContentID] ASC);
GO

-- Creating primary key on [LinkID] in table 'Links'
ALTER TABLE [dbo].[Links]
ADD CONSTRAINT [PK_Links]
    PRIMARY KEY CLUSTERED ([LinkID] ASC);
GO

-- Creating primary key on [NewsID] in table 'News'
ALTER TABLE [dbo].[News]
ADD CONSTRAINT [PK_News]
    PRIMARY KEY CLUSTERED ([NewsID] ASC);
GO

-- Creating primary key on [ElementID] in table 'Libraries'
ALTER TABLE [dbo].[Libraries]
ADD CONSTRAINT [PK_Libraries]
    PRIMARY KEY CLUSTERED ([ElementID] ASC);
GO

-- Creating primary key on [EmailID] in table 'TBEmails'
ALTER TABLE [dbo].[TBEmails]
ADD CONSTRAINT [PK_TBEmails]
    PRIMARY KEY CLUSTERED ([EmailID] ASC);
GO

-- Creating primary key on [Id] in table 'ImagesItems'
ALTER TABLE [dbo].[ImagesItems]
ADD CONSTRAINT [PK_ImagesItems]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [ItemID] in table 'Items'
ALTER TABLE [dbo].[Items]
ADD CONSTRAINT [PK_Items]
    PRIMARY KEY CLUSTERED ([ItemID] ASC);
GO

-- Creating primary key on [Id] in table 'ItemDetails'
ALTER TABLE [dbo].[ItemDetails]
ADD CONSTRAINT [PK_ItemDetails]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [CategoryID] in table 'Items'
ALTER TABLE [dbo].[Items]
ADD CONSTRAINT [FK_Item_Category]
    FOREIGN KEY ([CategoryID])
    REFERENCES [dbo].[Categories]
        ([CategoryID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Item_Category'
CREATE INDEX [IX_FK_Item_Category]
ON [dbo].[Items]
    ([CategoryID]);
GO

-- Creating foreign key on [ItemId] in table 'ImagesItems'
ALTER TABLE [dbo].[ImagesItems]
ADD CONSTRAINT [FK_ImagesItem_Item]
    FOREIGN KEY ([ItemId])
    REFERENCES [dbo].[Items]
        ([ItemID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ImagesItem_Item'
CREATE INDEX [IX_FK_ImagesItem_Item]
ON [dbo].[ImagesItems]
    ([ItemId]);
GO

-- Creating foreign key on [ItemId] in table 'ItemDetails'
ALTER TABLE [dbo].[ItemDetails]
ADD CONSTRAINT [FK_ItemDetails_Item]
    FOREIGN KEY ([ItemId])
    REFERENCES [dbo].[Items]
        ([ItemID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ItemDetails_Item'
CREATE INDEX [IX_FK_ItemDetails_Item]
ON [dbo].[ItemDetails]
    ([ItemId]);
GO

-- Creating foreign key on [ItemID] in table 'ItemProperties'
ALTER TABLE [dbo].[ItemProperties]
ADD CONSTRAINT [FK_ItemProperty_Item]
    FOREIGN KEY ([ItemID])
    REFERENCES [dbo].[Items]
        ([ItemID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ItemProperty_Item'
CREATE INDEX [IX_FK_ItemProperty_Item]
ON [dbo].[ItemProperties]
    ([ItemID]);
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------